<?php
class Application_Ajax_Server
{
	protected $_request;
	protected $_params;
	protected $_response;

	public function __construct() {
		$this->_init();
	}
	
	public function setRequest($request) {
		$this->_request = $this->_map($request);
	}

	public function setParams($params) {
		$this->_params = $params;
	}

	public function call() {
		try {
			@Zend_Loader::loadClass(ucfirst($this->_request), dirname(__FILE__).'/Server');
		} catch (Zend_Exception $ze) {
			header("HTTP/1.0 404 class not found: " . ucfirst($this->_request), true, 404);
			return false;
		}
		$function = new $this->_request($this->_params);
		$this->_response = $function->call();
		return (!$this->_response) ? false : true;
	}

	public function getResponse() {
		return $this->_response;
	}

	protected function _init() {
	
	}
	
	private function _map($function) {
		$legacyActions = array(
			"browse-term"						=> "BrowseTerm",
			"browse-all"						=> "BrowseAll",
			"get-construction-details-by-id"	=> "GetConstructionDetailsById",
			"add-construction-material"			=> "AddConstructionMaterial",
			"delete-construction"				=> "DeleteConstruction",
			"save-construction"					=> "SaveConstruction",
			"get-material-list"					=> "GetMaterialList",
			"get-material-categories-list"		=> "GetMaterialCategoriesList",
			"get-material-details-by-id"		=> "GetMaterialDetailsById",
			"delete-material"					=> "DeleteMaterial",
			"save-material"						=> "SaveMaterial",
			"get-pane-list"						=> "GetPaneList",
			"get-pane-categories-list"			=> "GetPaneCategoriesList",
			"get-pane-details-by-id"			=> "GetPaneDetailsById",
			"delete-pane"						=> "DeletePane",
			"save-pane"							=> "SavePane"
		);
		if (array_key_exists($function, $legacyActions)) {
			return $legacyActions[$function];
		}
		return $function;
	}
}

